package com.hc.tenant.service.impl;

import com.hc.tenant.domain.Dish;
import com.hc.tenant.mapper.DishMapper;
import com.hc.tenant.mapper.MenusDishMapper;
import com.hc.tenant.service.IDishService;
import com.hc.tenant.service.IMenusDishService;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MenusDishService implements IMenusDishService {

    @Autowired
    private MenusDishMapper menusDishMapper;
    @Autowired
    private IDishService dishService;

    @Override
    public List<Integer> selectDishIdsByMenusId(Integer menusId) {
        return menusDishMapper.selectDishByMenusId(menusId);
    }

    public List<Dish> selectDishesByMenusId(Integer menusId) {
        List<Integer> dishIds = menusDishMapper.selectDishByMenusId(menusId);
        if(dishIds.size()==0)
            return null;
        List<Dish> dishList = dishService.selectDishesByDishIds(dishIds);
        return dishList;
    }

    @Override
    @Transactional
    public int changeMenusDish(Integer menusId, List<Integer> dishIds) {
        deleteMenusDishByMenusId(menusId);
        if(dishIds == null || dishIds.size()==0)
            return 1;
        return menusDishMapper.insertMenusDish(menusId, dishIds);
    }

    @Override
    @Transactional
    public int deleteMenusDishByMenusId(Integer menusId) {
        return menusDishMapper.deleteMenusDishByMenusId(menusId);
    }
}
